package com.example.barcodeapp.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.barcodeapp.R;
import com.example.barcodeapp.interfaces.IBasePersenter;
import com.example.barcodeapp.interfaces.IBaseView;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity<P extends IBasePersenter> extends AppCompatActivity implements IBaseView {

    Unbinder unbinder;

    protected P persenter;
    private long lastClick = 0;
    protected Resources res;
    public ImageView ivBack, ivRight, ivRightTwo;
    public LinearLayout toSerach;
    public TextView tvTitle, tvLocation;
    public Toolbar toolbar;
    public int searchType = 1;
    public Context mContext;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设计模式中的模板方法调用
        setContentView(getLayout());
        res = getResources();
        unbinder = ButterKnife.bind(this);

        initView();
        persenter = initPersenter();
        if(persenter != null) {
            persenter.attachView(this);
            initData();
        }
    }

    /**
     * 获取界面布局的方法
     * @return
     */
    protected abstract int getLayout();

    /**
     * 初始化界面
     */
    protected abstract void initView();

    /**
     * 初始化p层
     */
    protected abstract P initPersenter();

    /**
     * 初始化加载数据
     */
    protected abstract void initData();


    @Override
    public void showTips(String tips) {

    }

    @Override
    public void showLoading(int visible) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(unbinder != null){
            unbinder.unbind();
        }
        if(persenter != null){
            persenter.detachView();
        }
    }
    public void setToolBar(boolean ivLeft, boolean location, boolean title, boolean search, boolean rightImage) {
        toolbar = findViewById(R.id.toolBar);
        toolbar.setTitle("");
        if (location) {
            tvLocation = findViewById(R.id.tv_location);
            tvLocation.setVisibility(View.VISIBLE);
        }
        if (title) {
            tvTitle = findViewById(R.id.tv_title);
            tvTitle.setVisibility(View.VISIBLE);

        }
        if (search) {
            toSerach = findViewById(R.id.ll_to_search);
            toSerach.setVisibility(View.VISIBLE);
            toSerach.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TTXD.goToSearch(BaseActivity.this, searchType);
                }
            });
        }
        if (rightImage) {
            ivRight = findViewById(R.id.right_iv);
            ivRight.setVisibility(View.VISIBLE);
        }
        setSupportActionBar(toolbar);
        if (ivLeft) {
            ivBack = findViewById(R.id.iv_back);
            ivBack.setVisibility(View.VISIBLE);
            ivBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    public void setCicleBack() {
        View view = findViewById(R.id.ff_back_contener);
        view.setVisibility(View.VISIBLE);
        ivBack.setVisibility(View.GONE);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void setToolbarRightImage(int ivOne, int ivTwo) {
        if (ivOne != 0) {
            ivRight = findViewById(R.id.right_iv);
            ivRight.setVisibility(View.VISIBLE);
            ivRight.setImageDrawable(res.getDrawable(ivOne));
        }
        if (ivTwo != 0) {
            ivRightTwo = findViewById(R.id.right_iv_two);
            ivRightTwo.setVisibility(View.VISIBLE);
            ivRightTwo.setImageDrawable(res.getDrawable(ivTwo));
        }
    }

    public void setTitle(String title, int textColor, int toolBarColor) {
        if (tvTitle != null) {
            tvTitle.setText(title);
            int color = res.getColor(textColor);
            tvTitle.setTextColor(color);
        }
        toolbar.setBackgroundColor(res.getColor(toolBarColor));
    }

    private static boolean isMiUi = false;
    @SuppressLint("PrivateApi")
    private void setMIUIStatusBarDarkMode() {
        if (isMiUi) {
            Class<? extends Window> clazz = getWindow().getClass();
            try {
                int darkModeFlag;
                Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                extraFlagField.invoke(getWindow(), darkModeFlag, darkModeFlag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
